﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Model;

public partial class Administration_test : System.Web.UI.Page
{
    protected Categories current;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            current = Entities.GetCategoryDetails(7);
            ViewState["Category"] = current;
            // parent category dropdown binder
            DataTable dt = Entities.GetTreeCategories();
            //ddlParentCateg.Items.Clear();
            int level = 0;
            RecursiveFillTree(dt, 0, level);
            ddlParentCateg.Items.Insert(0, new ListItem("Primul nivel", "0"));
            ddlParentCateg.SelectedValue = current.Parent.CategoryID.ToString();
            ddlParentCateg.DataBind();
        }
    }
    private void RecursiveFillTree(DataTable dtParent, int parentID, int level)
    {
        level++; //on the each call level increment 1
        System.Text.StringBuilder appender = new System.Text.StringBuilder();

        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|");
        }
        if (level > 0)
        {
            appender.Append("_ ");
        }

        current = (Categories)ViewState["Category"];
        if (parentID != current.CategoryID)
        {
            DataView dv = new DataView(dtParent);

            dv.RowFilter = string.Format("pid = {0}", parentID);

            int i;

            if (dv.Count > 0)
            {
                for (i = 0; i < dv.Count; i++)
                {
                    if (dv[i]["id"].ToString() != current.CategoryID.ToString())
                    {
                        ddlParentCateg.Items.Add(new ListItem(Server.HtmlDecode(appender.ToString() + dv[i]["Title"].ToString()), dv[i]["id"].ToString()));
                        RecursiveFillTree(dtParent, int.Parse(dv[i]["id"].ToString()), level);
                    }
                }
            }
            level--; //on the each function end level will decrement by 1
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //int? newParentID = null;
        //if (!string.IsNullOrEmpty(ddlParentCateg.SelectedItem.Value))
        //    newParentID = Convert.ToInt32(ddlParentCateg.SelectedItem.Value);
        Response.Write(ddlParentCateg.SelectedValue);
    }
}
